package com.education.welco.controller;

import com.education.common.pojo.AjaxResult;
import com.education.welco.feign.userFeign;
import com.education.welco.pojo.MineResCatalog;
import com.education.welco.pojo.MineResTable;
import com.education.welco.pojo.vo.MineResCatalogVO;
import com.education.welco.service.IMineResCatalogService;
import com.education.welco.service.IMineResTableService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 我的资源库目录 controller业务
 *
 * @Author: liftsail
 * @Date: 2022-10-17
 * @Description: 不积跬步无以至千里
 */
@RestController
@RequestMapping("/mineResCatalog")
public class MineResCatalogController {

    @Autowired
    private IMineResCatalogService mineResCatalogService;

    @Autowired
    private IMineResTableService mineResTableService;

    @Autowired
    private userFeign userFeign;


    /**
     * 列表查询我的资源库目录
     */
    @GetMapping("/list")
    public AjaxResult list(MineResCatalog mineResCatalog, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<MineResCatalog> list = mineResCatalogService.selectMineResCatalogList(mineResCatalog);
        PageInfo pageInfo = new PageInfo<>(list);
        return AjaxResult.success("查询成功！", pageInfo);
    }


    /**
     * 新增保存我的资源库目录
     * 图片1  视频2  文档3(资源目录类型catalog_type)
     */
    @PostMapping("/add")
    public AjaxResult addSave(@RequestBody MineResCatalog mineResCatalog) {
        //mineResCatalog||  pid  catalogName  catalogType  createId  createBy  createTime
        mineResCatalog.setCreateTime(new Date());
        mineResCatalogService.insertMineResCatalog(mineResCatalog);
        return AjaxResult.success("添加成功！");
    }


    /**
     * 主键详情我的资源库目录
     */
    @GetMapping("/selectOne")
    public AjaxResult selectOne(Integer id) {
        MineResCatalog result = mineResCatalogService.selectMineResCatalogById(id);
        return AjaxResult.success("查询成功！", result);
    }

    /**
     * 修改保存我的资源库目录
     */
    @PostMapping("/update")
    public AjaxResult editSave(@RequestBody MineResCatalog mineResCatalog) {
        mineResCatalogService.updateMineResCatalog(mineResCatalog);
        return AjaxResult.success("修改成功！");
    }

    /**
     * 删除我的资源库目录
     */
    @PostMapping("/del")
    public AjaxResult remove(@RequestBody Map idsMap) {
        String ids = String.valueOf(idsMap.get("ids"));
        int i = mineResCatalogService.deleteMineResCatalogByIds(ids);
        if (i == -1) return AjaxResult.error("请先删除子节点！");
        if (i == -2) return AjaxResult.error("当前节点下有资源！");
        return AjaxResult.success("删除成功！");
    }

    /**
     * 根据老师id和资源类型查询我的资源库左侧目录菜单
     *
     * @param teaId
     * @param type
     * @return
     */
    @GetMapping("/queryLeftMenuByTeaIdAndType")
    public AjaxResult queryLeftMenuByTeaIdAndType(@RequestParam("teaId") Integer teaId,
                                                  @RequestParam("resType") Integer type) {
        List<MineResCatalogVO> result = mineResCatalogService.queryLeftMenuByTeaIdAndType(teaId, type);
        return AjaxResult.success("查询成功！", result);
    }

    /**
     * 根据目录菜单的目录id查找资源表的资源
     *
     * @param catalogId
     * @param resName
     * @return
     */
    @GetMapping("/queryResListByCatalogId")
    public AjaxResult queryLeftMenuByTeaIdAndType(@RequestParam("catalogId") Integer catalogId, String resName, Integer pageNum, Integer pageSize) {
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<MineResTable> result = mineResTableService.queryResListByCatalogId(catalogId, resName);
        PageInfo<MineResTable> mineResTablePageInfo = new PageInfo<>(result);
        return AjaxResult.success(mineResTablePageInfo);
    }

    /**
     * 查找某一类型的全部资源    老师id  查找的资源type  , 资源表 目录 in 目录
     *
     * @param teaId
     * @param type
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/queryAllResListByTeaIdAndType")
    public AjaxResult queryAllResListByTeaIdAndType(@RequestParam("teaId") Integer teaId,
                                                    @RequestParam("resType") Integer type,
                                                    String resName,
                                                    Integer pageNum, Integer pageSize) {
        MineResCatalog mineResCatalog = new MineResCatalog();
        mineResCatalog.setCreateId(teaId);
        mineResCatalog.setCatalogType(String.valueOf(type));
        List<MineResCatalog> mineResCatalogVOList = mineResCatalogService.selectMineResCatalogList(mineResCatalog);
        List<Integer> catalogIdList = mineResCatalogVOList.parallelStream().map(mineResCatalogVO -> mineResCatalogVO.getCatalogId()).collect(Collectors.toList());
        if (catalogIdList != null && catalogIdList.size() == 0)
            return AjaxResult.success(new PageInfo<MineResTable>(new ArrayList<>()));
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<MineResTable> result = mineResTableService.queryAllResListByTeaIdAndType(catalogIdList, resName,null,type);
        PageInfo<MineResTable> mineResTablePageInfo = new PageInfo<>(result);
        return AjaxResult.success(mineResTablePageInfo);
    }

    /**
     * 逻辑同查全部资源(1-4、2-5、3-6)
     * 默认分组 购买的资源 pid0、catalogType4图片|5视频|6文档
     */

    /**
     * 备课中心-资源库上传-我的资源库资源(自己资源和购买资源)
     * 1-4、2-5、3-6
     *
     * @param teaId
     * @param type
     * @param resName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/queryMyResForPrepareCenter")
    public AjaxResult queryMyResForPrepareCenter(@RequestParam("teaId") Integer teaId,
                                                 @RequestParam("resType") Integer type,
                                                 String resName,
                                                 Integer pageNum, Integer pageSize) {
        /*MineResCatalog mineResCatalog = new MineResCatalog();
        mineResCatalog.setCreateId(teaId);
        mineResCatalog.setCatalogType(String.valueOf(type));
        List<MineResCatalog> mineResCatalogVOList = mineResCatalogService.selectMineResCatalogList(mineResCatalog);*/
        List<Integer> catalogTypeList = new ArrayList<>();
        if (type == 1) {
            catalogTypeList.add(1);
            catalogTypeList.add(4);
        } else if (type == 2) {
            catalogTypeList.add(2);
            catalogTypeList.add(5);
        } else if (type == 3) {
            catalogTypeList.add(3);
            catalogTypeList.add(6);
        }
        List<MineResCatalog> mineResCatalogVOList = mineResCatalogService.selectMineResCatalogListByCreateIdAndCatalogTypeList(teaId,catalogTypeList);
        List<Integer> catalogIdList = mineResCatalogVOList.parallelStream().map(mineResCatalogVO -> mineResCatalogVO.getCatalogId()).collect(Collectors.toList());
        if (catalogIdList != null && catalogIdList.size() == 0)
            return AjaxResult.success(new PageInfo<MineResTable>(new ArrayList<>()));
        if (pageNum != null && pageSize != null) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<MineResTable> result = mineResTableService.queryAllResListByTeaIdAndType(catalogIdList, resName,"1",type);
        PageInfo<MineResTable> mineResTablePageInfo = new PageInfo<>(result);
        return AjaxResult.success(mineResTablePageInfo);
    }

    //这是学校资源
    //192.168.3.79:7405/api/welco/platResourceShare/shareSchResList?schoolId=21&fileType=VIDEO&teaId=1207
    //DOC,VIDEO,IMAGE

    //默认分组catalog_id的查找 老师id和默认分组类型catalogType    有返回,无新增
    @GetMapping("/defaultGroupQueryAndReturn")
    public Map<String, Integer> defaultGroupQueryAndReturn(@RequestParam("teaId") Integer teaId) {
        Map<String, Object> mapUser = userFeign.findByUserId(teaId);
        String  teaName = (String)mapUser.get("name");
        HashMap<String, Integer> groupMap = new HashMap<>();
        //图片
        MineResCatalog mineResCatalog = mineResCatalogService.defaultGroupQueryAndReturn(teaId,4);
        if (null != mineResCatalog && null != mineResCatalog.getCatalogId()) {
            groupMap.put("4",mineResCatalog.getCatalogId());
        } else {
            MineResCatalog imagemineResCatalog = new MineResCatalog();
            imagemineResCatalog.setPid(0);
            imagemineResCatalog.setCatalogName("图片默认分组");
            imagemineResCatalog.setCatalogType("4");
            imagemineResCatalog.setCreateId(teaId);
            imagemineResCatalog.setCreateBy(teaName);
            imagemineResCatalog.setCreateTime(new Date());
            int i = mineResCatalogService.insertMineResCatalog(imagemineResCatalog);
            groupMap.put("4",imagemineResCatalog.getCatalogId());
        }
        //视频
        MineResCatalog mineResCatalog2 = mineResCatalogService.defaultGroupQueryAndReturn(teaId,5);
        if (null != mineResCatalog2 && null != mineResCatalog2.getCatalogId()) {
            groupMap.put("5",mineResCatalog2.getCatalogId());
        } else {
            MineResCatalog videomineResCatalog = new MineResCatalog();
            videomineResCatalog.setPid(0);
            videomineResCatalog.setCatalogName("视频默认分组");
            videomineResCatalog.setCatalogType("5");
            videomineResCatalog.setCreateId(teaId);
            videomineResCatalog.setCreateBy(teaName);
            videomineResCatalog.setCreateTime(new Date());
            int i = mineResCatalogService.insertMineResCatalog(videomineResCatalog);
            groupMap.put("5",videomineResCatalog.getCatalogId());
        }
    //    文档
        MineResCatalog mineResCatalog3 = mineResCatalogService.defaultGroupQueryAndReturn(teaId,6);
        if (null != mineResCatalog3 && null != mineResCatalog3.getCatalogId()) {
            groupMap.put("6",mineResCatalog3.getCatalogId());
        } else {
            MineResCatalog docmineResCatalog = new MineResCatalog();
            docmineResCatalog.setPid(0);
            docmineResCatalog.setCatalogName("文档默认分组");
            docmineResCatalog.setCatalogType("6");
            docmineResCatalog.setCreateId(teaId);
            docmineResCatalog.setCreateBy(teaName);
            docmineResCatalog.setCreateTime(new Date());
            int i = mineResCatalogService.insertMineResCatalog(docmineResCatalog);
            groupMap.put("6",docmineResCatalog.getCatalogId());
        }
        return groupMap;
    }
}